package me.model;

import java.util.ArrayList;

import me.Adventure.Adventure;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class Room {
	
	private Room(){
		// new Room(codigo, norte, leste, oeste, sul, cor)
		// new Room(screencode, north, east, west, south, color)
		roomList = new ArrayList<Room>(25);
		roomList.add(new Room(0, 1, 1, 1, 1, Color.orange));
		roomList.add(new Room(1, 0, 2, 2, 2, Color.orange));
		roomList.add(new Room(2, 1, 3, 3, 3, Color.green));
	};
	
	private Room(int screenCode, 
				 int north, 
				 int east, 
				 int west,
				 int south,
				 Color cor){

		this.setScreenCode(screenCode);
		this.setNorth(north);
		this.setEast(east);
		this.setWest(west);
		this.setSouth(south);
		this.setCor(cor);

	}

	private int screenCode;

	private int north;
	private int east;
	private int west;
	private int south;

	private Color cor;

	private Image imageRoom;
	private ArrayList<Room> roomList;
	
	private static Room instance = null;
	
	public static Room getInstance(){
		if(instance == null){
			instance = new Room();
		}
		
		return instance;
	}

	/**
	 * Cria a imagem da tela
	 * @throws SlickException 
	 */
	public Image imageRoom(int screencode) throws SlickException{

		Image imagem = new Image(Adventure.SCREEN_WIDTH, Adventure.SCREEN_HEIGHT);
		Graphics imagemG = imagem.getGraphics();
		String grade = getScreen(screencode).toString();
		
		int posX = 0;
		int posY = 0;
		int cont = 0;
		
		imagemG.setBackground(Color.gray);
		imagemG.clear();

		imagemG.setColor(getRoomColor(screencode));

		while(cont != grade.length()){

			if(isBrick(grade.charAt(cont))){
				imagemG.fillRect((posX), (posY), Adventure.squareWidth, Adventure.squareHeight);
			}

			posX+=Adventure.squareWidth;

			if(posX == Adventure.SCREEN_WIDTH){
				posX = 0;
				posY+= Adventure.squareHeight;
			}

			cont++;
		}

		imagemG.flush();
		return imagem;

	}

	private boolean isBrick(char letra){
		return (letra != '.')?true:false;
	}

	public int getScreenCode() {
		return screenCode;
	}

	public void setScreenCode(int code) {
		this.screenCode = code;
	}

	public int getNorth() {
		return north;
	}

	public void setNorth(int north) {
		this.north = north;
	}

	public int getEast() {
		return east;
	}

	public void setEast(int east) {
		this.east = east;
	}

	public int getWest() {
		return west;
	}

	public void setWest(int west) {
		this.west = west;
	}

	public int getSouth() {
		return south;
	}

	public void setSouth(int south) {
		this.south = south;
	}

	public Color getCor() {
		return cor;
	}

	public void setCor(Color cor) {
		this.cor = cor;
	}
	
	public Color getRoomColor(int screencode){
		return roomList.get(screencode).getCor();
	}
	
	public void changeRoom(int screencode){
		this.setScreenCode(screencode);
		this.setNorth(roomList.get(screencode).getNorth());
		this.setEast(roomList.get(screencode).getEast());
		this.setWest(roomList.get(screencode).getWest());
		this.setSouth(roomList.get(screencode).getSouth());
		this.setCor(roomList.get(screencode).getCor());
	}

	public StringBuilder getScreen(int screenCode){

		StringBuilder map = new StringBuilder();

		switch(screenCode){

		case 0:{
			map.append("0000000000000000000000000000000000000000");
			map.append("0000000000000000000000000000000000000000");
			map.append("0000000000000000000000000000000000000000");
			map.append("00....................................00");
			map.append("00....................................00");
			map.append("00....................................00");
			map.append("00....................................00");
			map.append("00....................................00");
			map.append("00....................................00");
			map.append("00....................................00");
			map.append("00....................................00");
			map.append("00....................................00");
			map.append("00....................................00");
			map.append("00....................................00");
			map.append("00....................................00");
			map.append("00....................................00");
			map.append("00....................................00");
			map.append("00....................................00");
			map.append("00....................................00");
			map.append("00....................................00");
			map.append("00....................................00");
			map.append("00....................................00");
			map.append("0000000000000000........0000000000000000");
			map.append("0000000000000000........0000000000000000");
			break;
		}

		case 1:{
			map.append("00000000000.0.0.0......0.0.0.00000000000");
			map.append("00000000000.0.0.0......0.0.0.00000000000");
			map.append("00........0000000......0000000........00");
			map.append("00........0000000......0000000........00");
			map.append("00........0000000......0000000........00");
			map.append("00........0000000......0000000........00");
			map.append("00........00000000000000000000........00");
			map.append("00........00000000000000000000........00");
			map.append("00........00000000000000000000........00");
			map.append("00........00000000000000000000........00");
			map.append("00..........0000000000000000..........00");
			map.append("00..........0000000000000000..........00");
			map.append("00..........0000000000000000..........00");
			map.append("00..........0000000000000000..........00");
			map.append("00..........000000.FA.000000..........00");
			map.append("00..........000000.FA.000000..........00");
			map.append("00..........000000.FA.000000..........00");
			map.append("00..........000000.FA.000000..........00");
			map.append("00....................................00");
			map.append("00....................................00");
			map.append("00....................................00");
			map.append("00....................................00");
			map.append("0000000000000000........0000000000000000");
			map.append("0000000000000000........0000000000000000");
			break;
		}

		case 2:{
			map.append("0000000000000000........0000000000000000");
			map.append("0000000000000000........0000000000000000");
			map.append("0000000000000000........0000000000000000");
			map.append("........................................");
			map.append("........................................");
			map.append("........................................");
			map.append("........................................");
			map.append("........................................");
			map.append("........................................");
			map.append("........................................");
			map.append("........................................");
			map.append("........................................");
			map.append("........................................");
			map.append("........................................");
			map.append("........................................");
			map.append("........................................");
			map.append("........................................");
			map.append("........................................");
			map.append("........................................");
			map.append("........................................");
			map.append("........................................");
			map.append("0000000000000000000000000000000000000000");
			map.append("0000000000000000000000000000000000000000");
			map.append("0000000000000000000000000000000000000000");
			break;
		}

		}

		return map;

	}

}
